package com.cuit.view;

import com.cuit.domain.User;
import com.cuit.service.UserService;

import java.util.Scanner;

public class UserView {
    private UserService userService = new UserService();

    public void run() {
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        while (flag) {
            System.out.println("--------------------记账系统--------------------");
            System.out.println("1. 用户登录 2. 注册账户 3. 退出系统");
            System.out.println("请输入要操作的功能号[1-3]...");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    login();
                    break;
                case 2:
                    register();
                    break;
                case 3:
                    System.out.println("欢迎下次使用！");
                    flag = false;
                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    private void login() {
        Scanner sc = new Scanner(System.in);
        User user = new User();
        System.out.println("请输入用户名：");
        user.setUsername(sc.nextLine());
        System.out.println("请输入密码：");
        user.setPassword(sc.nextLine());
        int i = userService.login(user);
        if (i == 0) {
            System.out.println("用户名或密码错误");
        }
        if (i == 1) {
            System.out.println("登陆成功");
            new MainView(user).run();
        }
    }

    private void register() {
        Scanner sc = new Scanner(System.in);
        User user = new User();
        System.out.println("请输入用户名：");
        user.setUsername(sc.nextLine());
        System.out.println("请输入密码：");
        user.setPassword(sc.nextLine());
        int i = userService.register(user);
        if (i == 0) {
            System.out.println("注册失败");
        }
        if (i == 1) {
            System.out.println("注册成功");
        }
    }
}
